//
//  HEAIMultiQuestionCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/23.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEAIMultiQuestionCell.h"
#import "HEAIMultiSelectCell.h"


@interface HEAIMultiQuestionCell ()<UITableViewDataSource, UITableViewDelegate >

@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UILabel *titleLb;

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) HEAIMsgModel *testModel;
@property (nonatomic, strong) NSIndexPath *indexPath;


@property (nonatomic, strong) UIView *warp;
@property (nonatomic, strong) UIView *btnWarp;

@end

@implementation HEAIMultiQuestionCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = CViewBgColor;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    // 商品图片
    _leftImageView = [UIImageView new];
    _leftImageView.image = IMAGE_NAMED(@"fit_server");
    _leftImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:_leftImageView];
    
    UIView *warp = [UIView new];
    warp.backgroundColor = KWhiteColor;
    [self.contentView addSubview:warp];
    _warp = warp;
    
    warp.layer.shadowColor = [UIColor colorWithRed:13/255.0 green:98/255.0 blue:218/255.0 alpha:0.08].CGColor;
    warp.layer.shadowOffset = CGSizeMake(0,5);
    warp.layer.shadowOpacity = 1;
    warp.layer.shadowRadius = 6;
    warp.layer.cornerRadius = 10;
    
    _titleLb = [UILabel new];
    _titleLb.text = @"题目";
    _titleLb.font = DYFont(16);
    _titleLb.textColor = CNavLightBlackColor;
    _titleLb.numberOfLines = 0;
    [warp addSubview:_titleLb];
    
 
    [_leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.top.equalTo(self.contentView).mas_offset(15);
       make.left.equalTo(self.contentView).mas_offset(15);
       make.width.height.mas_offset(50);
    }];
    [_leftImageView hg_setAllCornerWithCornerRadius: 20];
    
    
    [warp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).mas_offset(15);
        make.left.equalTo(_leftImageView.mas_right).mas_offset(15);
        make.right.equalTo(self.contentView).mas_offset( -(15 + 50 + 5));
//        make.height.mas_greaterThanOrEqualTo(40);
        make.bottom.equalTo(self.contentView).mas_offset(-15);
    }];
     
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(warp).mas_offset(15);
        make.left.equalTo(warp).mas_offset(15);
        make.right.equalTo(warp).mas_offset( -15);
    }];
    
    _tableView = [[GDTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, CGFLOAT_MIN)];
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0)];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollsToTop = YES;
    [warp addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).offset(0);
        make.left.equalTo(warp).offset(15);
        make.right.equalTo(warp).offset(-15);
    }];
    
    [_tableView registerClass:HEAIMultiSelectCell.class forCellReuseIdentifier: KHEAIMultiSelectCell];
    
    
    // 底部按钮
    UIView *btnWarp = [UIView new];
    [self.contentView addSubview:btnWarp];
    _btnWarp = btnWarp;
    
    [btnWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_tableView.mas_bottom).mas_offset(20);
//        make.right.equalTo(warp).mas_offset(-0);
//        make.left.equalTo(warp).mas_offset(0);
        
        make.width.mas_offset(90*2 + 20);
        make.centerX.equalTo(warp);
        make.height.mas_offset(34);
        make.bottom.equalTo(warp).mas_offset(-10);
    }];
    
    QMUIGhostButton *refuseBtn = [[QMUIGhostButton alloc] initWithGhostColor:CNavBgColor];
    refuseBtn.titleLabel.font = UIFontMake(16);
    [refuseBtn setTitle:@"提前结束" forState:UIControlStateNormal];
    [btnWarp addSubview:refuseBtn];

    QMUIFillButton *agreeBtn = [[QMUIFillButton alloc] initWithFillColor:CNavBgColor titleTextColor:CNavBgFontColor];
    agreeBtn.titleLabel.font = UIFontMake(16);
    [agreeBtn setTitle:@"提交" forState:UIControlStateNormal];
    [btnWarp addSubview:agreeBtn];
    
    [refuseBtn addTarget:self action:@selector(endCLick) forControlEvents:UIControlEventTouchUpInside];
    [agreeBtn addTarget:self action:@selector(answerClick) forControlEvents:UIControlEventTouchUpInside];
    
//    NSArray *arr = @[refuseBtn, agreeBtn];
//
//    [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:30 leadSpacing:20 tailSpacing:20];
//    [arr mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(btnWarp);
//        make.bottom.equalTo(btnWarp);
//    }];

    [refuseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(btnWarp);
        make.left.equalTo(btnWarp);
        make.width.mas_offset(90);
        make.height.mas_offset(34);
    }];

    [agreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(btnWarp);
        make.right.equalTo(btnWarp).mas_offset(0);
        make.width.mas_offset(90);
        make.height.mas_offset(34);
    }];
}

- (void)endCLick {
    
    self.testModel.isFinish = YES;
    [self.testModel refreshData];
    if ([_delegate respondsToSelector:@selector(endQuestionSingleWith:)]) {
        [_delegate endQuestionSingleWith: self.testModel];
    }
}

- (void)answerClick {

    NSMutableArray *selectArr = @[].mutableCopy;
    for (HEAISubItemModel *item in self.testModel.subDataArr) {
        if (item.isChoose) {
            [selectArr addObject:item.content];
        }
    }
    
    if (selectArr.count == 0) {
        [Dialog toastBottom:@"请选择您的答案"];
        return;
    }
    
    self.testModel.isFinish = YES;
    [self.testModel refreshData];
    self.testModel.userAnswerContent = [selectArr componentsJoinedByString:@","];
    
    if ([_delegate respondsToSelector:@selector(answerQuestionSingleWith:)]) {
        [_delegate answerQuestionSingleWith: self.testModel];
    }
}

- (void)refreshContent:(GDFiterCellModel *)fiterModel formModel:(GDFormModel *)formModel indexPath:(NSIndexPath *)indexPath {
    
    if (formModel == nil) {
        return;
    }
    _delegate = formModel.proxyVC;
    self.testModel = formModel.zx_model;
    
    _titleLb.text = [NSString stringWithFormat:@"%@（多选）", self.testModel.content];

    // refreshContent 会走两次 但只有第一次计算的是正确的，故以此方法解决 奇怪
    CGFloat tableViewHeight = [self.testModel getTableHeight:_tableView];
    
    DLog(@"%f", tableViewHeight)
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
       make.height.mas_equalTo(tableViewHeight);
    }];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView reloadData];
    
    if (!self.testModel.isFinish) {
        _btnWarp.hidden = NO;
        [_btnWarp mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_tableView.mas_bottom).mas_offset(20);
            make.height.mas_offset(34);
        }];
    }else {
        _btnWarp.hidden = YES;
        [_btnWarp mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_tableView.mas_bottom).mas_offset(0);
            make.height.mas_offset(0);
        }];
    }
}
 

#pragma mark -

- (void)changeClick {
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HEAIMultiSelectCell *cell = [tableView dequeueReusableCellWithIdentifier: KHEAIMultiSelectCell];
  HEAISubItemModel *model = [self.testModel.subDataArr objectAtIndex:indexPath.row];
  [cell configCellWithModel:model];
  return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.testModel.subDataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HEAISubItemModel *model = [self.testModel.subDataArr objectAtIndex:indexPath.row];

    CGFloat cellHeight = [tableView fd_heightForCellWithIdentifier: KHEAIMultiSelectCell cacheByIndexPath:indexPath configuration:^(HEAIMultiSelectCell *cell) {
        [cell configCellWithModel:model];
            }];
    return cellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEAISubItemModel *model = [self.testModel.subDataArr objectAtIndex:indexPath.row];

    if (!self.testModel.isFinish) {
        model.isChoose = !model.isChoose;
    }
    [_tableView reloadData];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  return YES;
}

#pragma mark - cell
 
- (void)resetChooseSatus {
    for (HEAISubItemModel *item in self.testModel.subDataArr) {
        item.isChoose = NO;
    }
}


@end
